package org.potomak.patientmanager.beans.model;

import java.io.Serializable;
import java.util.Set;
import java.util.HashSet;
import java.util.Date;
import java.text.DateFormat;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import javax.jdo.listener.DeleteCallback;


public class PatientBean implements Serializable, DeleteCallback {

	private int id;
	private String name;
	private Date birthDay;
	private String gender;
	private Double height;
	private Double weight;
	private Date date;
	private Set checkUps;
	private Set simpleNotes;
        private Set importantNotes;
        private Set suggestionNotes;
	private Set attachments;

	public PatientBean() {
		name = "Tizio Caio";
		birthDay = new Date();
		gender = "M";
		height = new Double(1.89);
		weight = new Double(76.0);
		date = new Date();
		checkUps = new HashSet();
		simpleNotes = new HashSet();
                importantNotes = new HashSet();
                suggestionNotes = new HashSet();
		attachments = new HashSet();
	}

	public PatientBean(String name, Date birthDay, String gender, Double height, Double weight, Date date, Set checkUps, Set simpleNotes, Set importantNotes, Set suggestionNotes, Set attachments) {
		this.name = name;
		this.birthDay = birthDay;
		this.gender = gender;
		this.height = height;
		this.weight = weight;
		this.date = date;
		this.checkUps = checkUps;
		this.simpleNotes = simpleNotes;
                this.importantNotes = importantNotes;
                this.suggestionNotes = suggestionNotes;
		this.attachments = attachments;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getBirthDay() {
		return birthDay;
	}

	public void setBirthDay(Date birthDay) {
		this.birthDay = birthDay;
	}
	
	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}
	
	public Double getHeight() {
		return height;
	}

	public void setHeight(Double height) {
		this.height = height;
	}

	public Double getWeight() {
		return weight;
	}

	public void setWeight(Double weight) {
		this.weight = weight;
	}
	
	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}
	
	public void addCheckUp(CheckUpBean checkUp) {
		checkUps.add(checkUp);
	}

	public void removeCheckUp(CheckUpBean checkUp) {
		checkUps.remove(checkUp);
	}

	public Set getCheckUps() {
		return checkUps;
	}

	public int getNumberOfCheckUps() {
		return checkUps.size();
	}
	
	public void addSimpleNote(SimpleNoteBean simpleNote) {
		simpleNotes.add(simpleNote);
	}

	public void removeSimpleNote(SimpleNoteBean simpleNote) {
		simpleNotes.remove(simpleNote);
	}

	public Set getSimpleNotes() {
		return simpleNotes;
	}

	public int getNumberOfSimpleNotes() {
		return simpleNotes.size();
	}
        
        public void addImportantNote(ImportantNoteBean importantNote) {
		importantNotes.add(importantNote);
	}

	public void removeImportantNote(ImportantNoteBean importantNote) {
		importantNotes.remove(importantNote);
	}

	public Set getImportantNotes() {
		return importantNotes;
	}

	public int getNumberOfImportantNotes() {
		return importantNotes.size();
	}
        
        public void addSuggestionNote(SuggestionNoteBean suggestionNote) {
		suggestionNotes.add(suggestionNote);
	}

	public void removeSuggestionNote(SuggestionNoteBean suggestionNote) {
		suggestionNotes.remove(suggestionNote);
	}

	public Set getSuggestionNotes() {
		return suggestionNotes;
	}

	public int getNumberOfSuggestionNotes() {
		return suggestionNotes.size();
	}
	
	public void addAttachment(AttachmentBean attachment) {
		attachments.add(attachment);
	}

	public void removeAttachment(AttachmentBean attachment) {
		attachments.remove(attachment);
	}

	public Set getAttachments() {
		return attachments;
	}

	public int getNumberOfAttachments() {
		return attachments.size();
	}

	public String toString() {
		return "name: " + name + " (" + gender + ") birth: " + DateFormat.getDateInstance().format(birthDay);
	}

    public void jdoPreDelete() {
        System.out.println("jdoPreDelete start");
        PersistenceManager myPM = JDOHelper.getPersistenceManager(this);
        
        Object[] simpleNotesElements = simpleNotes.toArray();
        Object[] importantNotesElements = importantNotes.toArray();
        Object[] suggestionNotesElements = suggestionNotes.toArray();
        Object[] checkUpsElements = checkUps.toArray();
        Object[] attachmentsElements = attachments.toArray();

        Transaction txn = myPM.currentTransaction();
        try {
            myPM.deletePersistentAll(simpleNotesElements);
            myPM.deletePersistentAll(importantNotesElements);
            myPM.deletePersistentAll(suggestionNotesElements);
            myPM.deletePersistentAll(checkUpsElements);
            myPM.deletePersistentAll(attachmentsElements);

            txn.commit();
        } finally {
            if (txn.isActive()) {
                txn.rollback();
            }
        }
        
        simpleNotes.clear();
        importantNotes.clear();
        suggestionNotes.clear();
        checkUps.clear();
        attachments.clear();

        txn.begin();
        
        System.out.println("jdoPreDelete end");
    }

}